var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    res.send('<h1>Welcome Realtime Server</h1>');
});
app.get('/index',function(req,res){
	res.sendfile('index.html')
})
  var users=[]
io.on('connection', function(socket){
     console.log('a user connected');
     socket.broadcast.emit('message',{msg:'你的好某XXX上线了'});
     
    // console.log(socket.id)
    users.push(socket.id)
    console.log("当前在线总人数为"+users.length)
    socket.on("disconnect", function() {
        console.log("a user go out");
        socket.broadcast.emit('leave',{msg:'你的好某XXX偷偷溜了'});
        users.pop()
        console.log("当前在线总人数为"+users.length)
    });
    socket.on('eventA',function(obj){
        alert(obj)
    })
    socket.on("eventB", function(obj) {
    	console.log(socket.id+":",obj)
        io.emit("eventC",{users,obj:obj,id:socket.id});
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});	